package com.diandiansong.app.ui.index;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.diandiansong.app.App;
import com.diandiansong.app.R;
import com.diandiansong.app.entity.CommodityInfoInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.Api;
import com.diandiansong.app.widgets.AddAndMinusViewInItem;

import java.util.List;

import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

/**
 * 畅销榜
 */
public class BestSelling extends BaseAct {

    public static void start(Context context) {
//        if (!LoginInfo.hasToken(context)) {
//            Login.start(context);
//            return;
//        }
        Intent starter = new Intent(context, BestSelling.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addFragment(new SubFragment());
        initTitle("畅销榜");
    }


    public static class SubFragment extends FastListFragment {

        @Override
        public int getItemLayoutRes() {
            return R.layout.good_item_add_minus;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {
            CommodityInfoInfo.Data item = (CommodityInfoInfo.Data) getItem(position);

            ImageView iv = findViewById(convertView, R.id.iv_logo);
            ImgLoader.display(iv, item.getImgs());

            fastText(convertView, R.id.tv_1, item.getCommodityName());
            fastText(convertView, R.id.tv_2, item.getCommoditySubtitle());


            String unit = item.getUnit();
            if (unit == null || unit.isEmpty() || unit.equals("/")) {
                unit = "";
            } else {
                unit = "/" + unit;
            }

            fastText(convertView, R.id.tv_3, "￥" + item.getPrice() + unit);

            fastVisibility(convertView, R.id.tv_buyer_count_label, View.VISIBLE);
            fastVisibility(convertView, R.id.tv_buyer_count, View.VISIBLE);
            fastText(convertView, R.id.tv_buyer_count, item.getSold() + "人购买");

            App.initGoodListAdd(getActivity(), item.getCommodityId(), convertView);
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            A.commodityBestselling(mPage, new CCallBack<CommodityInfoInfo>() {
                @Override
                public void onSuccess(CommodityInfoInfo o) throws Exception {
                    List<CommodityInfoInfo.Data> data = o.getData();
                    if (mPage.equals("0")) {
                        mAdapter.setData(data);
                    } else {
                        mAdapter.addAll(data);
                    }
                    if (mAdapter.getDataSize() > 0) {
                        mPage = ((CommodityInfoInfo.Data) getLast()).getSold();
                    }
                    onLoadFinishHasMore(data.size() >= Api.PAGE_SIZE);
                }

                @Override
                public void onFailure(int code, String msg) {
                    onLoadFinishNoMore();
                    toast(msg);
                }
            });
        }

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            AddAndMinusViewInItem aamvii = view.findViewById(R.id.add_minus);
            CommodityInfoInfo.Data item = (CommodityInfoInfo.Data) getItem(position);
            GoodDetail.start(getActivity(), item.getCommodityId());
        }
    }
}
